package com.assess.web.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.assess.web.dao.RandomDataMapper;
import com.assess.web.model.Assessment;
import com.assess.web.model.RandomData;
import com.assess.web.model.RandomDataExample;
import com.assess.web.model.Scheme;
import com.assess.web.service.AssessmentService;
import com.assess.web.service.RandomDataService;
import com.assess.web.service.SchemeService;
import com.assess.web.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RandomDataServiceImpl implements RandomDataService {


    @Autowired
    private AssessmentService assessmentService;
    @Autowired
    private SchemeService schemeService;
    @Autowired
    private RandomDataMapper randomDataMapper;

    @Override
    public String save(int assessmentId, Integer phasesId) {
        Assessment assessment = assessmentService.getAssessment(assessmentId);
        JSONArray json = JSONObject.parseArray(assessment.getRule());

        try {
            if (json.size() > 0) {
                Map<Integer, String> sosMap = new HashMap<>();
                Integer parentId = null;
                StringBuilder sbds = new StringBuilder();
                for (int j = 0; j < json.size(); j++) {
                    JSONArray so = json.getJSONArray(j);
                    for (int i = 0; i < so.size(); i++) {
                        JSONObject jsonObject = so.getJSONObject(i);
                        Scheme scheme = new Scheme();
                        String[] values = null;
                        for (String a : jsonObject.keySet()) {
                            if (a.equals("id")) {
                                scheme = schemeService.getScheme((Integer.valueOf(jsonObject.get(a).toString())));
                            } else {
                                values = jsonObject.get("value").toString().split("-");
                            }
                        }

                        if (null == values) {
                            return "";
                        }
                        int min = Integer.valueOf(values[0]);
                        int max = Integer.valueOf(values[1]);
                        StringBuilder sc = new StringBuilder();
                        for (int a = min; a < max; a++) {
                            sc.append(a).append(scheme.getName()).append(",");
                        }
                        parentId = scheme.getParentId();
                        sosMap.put(scheme.getId(), sc.toString());
                    }
                    StringBuilder sbd = new StringBuilder();
                    if (null != parentId) {
                        List<Scheme> schemeList = schemeService.listScheme(parentId);
                        Random rand = new Random();
                        for (Scheme scheme : schemeList) {
                            String s = sosMap.get(scheme.getId());
                            if (!StringUtil.isEmpty(s)) {
                                String[] sb = s.split(",");
                                sbd.append(sb[rand.nextInt(sb.length)]);
                            }
                        }
                    }
                    sbds.append(sbd).append(",");
                }
                if (!StringUtil.isEmpty(sbds.toString())) {
                    Random rand = new Random();
                    String[] sb = sbds.toString().split(",");

                    String a = sb[rand.nextInt(sb.length)];
                    RandomData randomData = new RandomData();
                    randomData.setRandomData(a);
                    randomData.setAssessment(assessmentId);
                    randomDataMapper.insertSelective(randomData);
                    return a;
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<RandomData> list(int assessmentId) {
        RandomDataExample example = new RandomDataExample();
        example.createCriteria().andAssessmentEqualTo(assessmentId);
        return randomDataMapper.selectByExample(example);
    }
}